﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DSLOrder
{
    public partial class FormEdit : Form
    {
        public FormEdit()
        {
            InitializeComponent();
        }
        Common Init = new Common();
        MsSqlHelper mssql = new MsSqlHelper();

        public string nationalMedicine = string.Empty;
        public string manufacturer = string.Empty;
        public string itemId = string.Empty;
        public string itemName = string.Empty;
        public string ypgg = string.Empty;
        public string dw = string.Empty;
        public string price = string.Empty;
        public string nprice = string.Empty;

        private void FormEdit_Load(object sender, EventArgs e)
        {
            PublicClass.Initialize();

            txtnationalMedicine.Text = nationalMedicine;
            txtmanufacturer.Text = manufacturer;
            txtitemId.Text = itemId;
            txtitemName.Text = itemName;
            txtypgg.Text = ypgg;
            txtdw.Text = dw;
            txtoprice.Text = price;
        }

        protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
        {
            int WM_KEYDOWN = 256;
            int WM_SYSKEYDOWN = 260;
            if (msg.Msg == WM_KEYDOWN | msg.Msg == WM_SYSKEYDOWN)
            {
                switch (keyData)
                {
                    case Keys.Escape:
                        this.Close();//esc关闭窗体
                        break;
                }
            }
            return false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string sql = "";
            nprice=txtnprice.Text;

            if(mssql.Getbool("select top 1 1 from DSL_Price where spid ='"+itemId+"'"))
            {
                sql = "update DSL_Price set price='" + nprice + "' where spid='" + itemId + "'";
            }else{
                sql = "insert into DSL_Price(spid,price)values('" + itemId + "','" + nprice + "')";
            }
            if (mssql.ExecuteSql(sql) > 0)
            {
                PublicClass.Instance.price = nprice;
                this.Close();
                this.Dispose();
            }
        }

        private void btncancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定删除这条数据？", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                string sql = "";
                nprice = txtnprice.Text;

                if (mssql.Getbool("select top 1 1 from DSL_Price where spid ='" + itemId + "'"))
                {
                    sql = "delete from DSL_Price where spid='" + itemId + "'";
                }
                if (mssql.ExecuteSql(sql) > 0)
                {
                    PublicClass.Instance.price = nprice;
                    this.Close();
                    this.Dispose();
                }
            }
        }
    }
}
